package com.project.nimsen.weixin.utils;

import java.security.MessageDigest;
import java.util.Arrays;
/**
 * 微信验证类
 * */
public class SignUtil {
	public static boolean checkSignature(String token, String signature, String timestamp, String nonce) throws Exception{
		String[] arr = { token, timestamp, nonce };
		Arrays.sort(arr);
		StringBuilder content = new StringBuilder();
		for (int i = 0; i < arr.length; i++) {
			content.append(arr[i]);
		}
		MessageDigest md = MessageDigest.getInstance("SHA-1");
		byte[] digest = md.digest(content.toString().getBytes());
		String tmpStr = byteToStr(digest);
		return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
	}
	private static String byteToStr(byte[] byteArray){
		StringBuffer strDigest = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			strDigest.append(byteToHexStr(byteArray[i]));
		}
		return strDigest.toString();
	}
	private static String byteToHexStr(byte mByte){
		char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		char[] tempArr = new char[2];
		tempArr[0] = Digit[(mByte >>> 4 & 0xF)];
		tempArr[1] = Digit[(mByte & 0xF)];
		String s = new String(tempArr);
		return s;
	}
	/**
	 * 
	 * 微信二维码生成的字符串
	 * MECARD:N:杜冰雪;ORG:应用搜;DIV:t.qq.com/wolf2leader;TEL:18103896062;EMAIL:accpdubing@gmail.com;ADR:郑州市经三路68号金成国际广场a座1205号;URL:http://www.yingyong.so;NOTE:QQ:258640006;
	 * 
	 * */
}
